package cn.iocoder.yudao.module.mqtt.controller.admin.mqtt;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.mqtt.MqttConfigDO;
import cn.iocoder.yudao.module.mqtt.service.MultiMqttMessageSender;
import cn.iocoder.yudao.module.mqtt.service.mqtt.MqttConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * MQTT使用示例 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - MQTT使用示例")
@RestController
@RequestMapping("/mqtt/example")
@Validated
public class MqttExampleController {

    @Autowired
    private MultiMqttMessageSender multiMqttMessageSender;

    @Autowired
    private MqttConfigService mqttConfigService;

    @PostMapping("/create-sample-config")
    @Operation(summary = "创建示例MQTT配置")
    @PreAuthorize("@ss.hasPermission('mqtt:config:create')")
    public CommonResult<Long> createSampleConfig() {
        // 创建示例配置
        MqttConfigDO config = MqttConfigDO.builder()
                .name("示例MQTT服务器")
                .username("admin")
                .password("123456")
                .url("tcp://localhost:1883")
                .subClientId("sub_client_sample")
                .subTopics("test/topic1,test/topic2")
                .pubClientId("pub_client_sample")
                .enabled(true)
                .description("这是一个示例MQTT配置")
                .build();

        Long configId = mqttConfigService.createConfig(config);
        return success(configId);
    }

    @PostMapping("/send-sample-message")
    @Operation(summary = "发送示例消息")
    @PreAuthorize("@ss.hasPermission('mqtt:message:send')")
    public CommonResult<Boolean> sendSampleMessage(
            @RequestParam(value = "configId", defaultValue = "1") Long configId,
            @RequestParam(value = "topic", defaultValue = "test/hello") String topic,
            @RequestParam(value = "message", defaultValue = "Hello MQTT!") String message) {
        
        multiMqttMessageSender.sendMsg(configId, topic, message);
        return success(true);
    }

    @GetMapping("/configs")
    @Operation(summary = "获取所有MQTT配置")
    @PreAuthorize("@ss.hasPermission('mqtt:config:query')")
    public CommonResult<List<MqttConfigDO>> getAllConfigs() {
        List<MqttConfigDO> configs = mqttConfigService.getEnabledConfigs();
        return success(configs);
    }

    @PostMapping("/test-connection")
    @Operation(summary = "测试MQTT连接")
    @PreAuthorize("@ss.hasPermission('mqtt:config:manage')")
    public CommonResult<Boolean> testConnection(@RequestParam("configId") Long configId) {
        // 检查实例是否运行
        boolean isRunning = mqttConfigService.isInstanceRunning(configId);
        
        if (!isRunning) {
            // 尝试启动实例
            mqttConfigService.startInstance(configId);
            isRunning = mqttConfigService.isInstanceRunning(configId);
        }
        
        return success(isRunning);
    }
} 